﻿namespace Softserve.Pottery.Manufacturing
{
    using System;
    using System.Runtime.Serialization;
    using System.Xml;
    using Softserve.Pottery.Infrastructure;
    using Softserve.Pottery.Infrastructure.Interfaces;
    using Softserve.Pottery.Infrastructure.Serialization;
    using Softserve.Pottery.Manufacturing.Domains;


    /// <summary>
    /// TODO: We need implementation of Repository
    /// </summary>    
    public class ManufacturingRepository : IRepository, ITestServiceModel
    {
        private const string App_Data = "Manufacturing";

        #region IRepository Members

        public string GetStorageName<T>() where T : class
        {
            throw new System.NotImplementedException();
        }

        public bool IsInitialized<T>() where T : class
        {
            throw new System.NotImplementedException();
        }

        public void Initialize<T>(string connectionString) where T : class
        {
            throw new System.NotImplementedException();
        }

        public void Add<T>(T tableEntity) where T : class, IDomain
        {
            XmlController.AddDomain<T>(tableEntity);
        }

        public void Update<T>(T tableEntity) where T : class, IDomain
        {
            XmlController.UpdateDomain<T>(tableEntity);
        }

        public void Delete<T>(T tableEntity) where T : class, IDomain
        {
            XmlController.RemoveDomain<T>(tableEntity.Id);
        }

        public T Create<T>() where T : class, IDomain
        {
            if (typeof(T) == typeof(Approvement))
            {
                return new Approvement() as T;
            }

            if (typeof(T) == typeof(Batch))
            {
                return new Batch() as T;
            }

            if (typeof(T) == typeof(Plan))
            {
                return new Plan() as T;
            }

            if (typeof(T) == typeof(Team))
            {
                return new Team() as T;
            }

            throw new NotSupportedException("Entity type is not supported");
        }

        public T Item<T, Key>(Key key) where T : class
        {
            throw new System.NotImplementedException();
        }

        public System.Linq.IQueryable<T> GetQuery<T>() where T : class
        {
            throw new System.NotImplementedException();
        }

        public System.Collections.Generic.IEnumerable<T> Enumerate<T>() where T : class
        {
            return XmlController.LoadCollection<T>();
        }

        #endregion

        #region IDisposable Members

        ~ManufacturingRepository()
        {
            Dispose(false);
        }
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        private void Dispose(bool disposeManaged)
        {
            ///TODO: Implement code of release resources here
        }

        #endregion

        #region ITestServiceModel Members

        public IPathResolver Resolver
        {
            get
            {
                return Resolver;
            }
            set
            {
                Resolver = value;
            }
        }

        public void LoadTestData(string caseName)
        {
            throw new NotImplementedException();
        }       

        public bool AnalysisResults()
        {
            throw new System.NotImplementedException();
        }

        #endregion

        private T[] LoadTypeCollection<T>(string caseName)
        {
            var ds = new DataContractSerializer(typeof(T[]));
            var settings = new XmlReaderSettings { };
            using (var r = XmlReader.Create(string.Format("{0}\\{1}\\{2}.xml", Resolver.ResolvePath(typeof(T).Name), settings)))
            {
                return ds.ReadObject(r) as T[];
            }
        }       
    }
}
